var util = require('../../../../utils/util.js');
var api = require('../../../../config/api.js');
var app = getApp();
var lastTime = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    duihuanList: [],
    fukucunsale: false,
    memberinfo: {},
    page: 1,
    limit: 10
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getmemberinfo();
    this.getduihuanList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getmemberinfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getduihuanList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getduihuanList: function () {
    let that = this;
    util.request(api.Jifenduihuan, {
      page: that.data.page,
      limit: that.data.limit
    }).then(function (res) {
      if (res.result == 1) {
        if (res.data.duihuan.length > 0) {
          that.setData({
            duihuanList: that.data.duihuanList.concat(res.data.duihuan),
            page: that.data.page + 1
          });
          that.setData({
            fukucunsale: res.data.fukucunsale
          })
        } else {
          wx.showToast({
            title: '已经被您看完了!',
            icon: 'none',
            duration: 2000
          });
          return false;
        }
      }
    });
  },
  getmemberinfo: function () {
    let that = this;
    util.request(api.MemberInfo).then(function (res) {
      if (res.result == 1) {
        that.setData({
          memberinfo: res.data
        });
      }
    });
  },
  /**
   * 去兑换商品
   */
  quduihuan: function (e) {
    let that = this;
    var duihuanid = e.currentTarget.dataset.duihuanid;
    var jifen = e.currentTarget.dataset.jifen;
    var jine = e.currentTarget.dataset.jine;
    var kucun = e.currentTarget.dataset.kucun;
    if (!jine) {
      jine = 0.00;
    }
    if (kucun <= 0 && !that.data.fukucunsale) {
      wx.showToast({
        title: '商品已被兑完！',
        icon: 'none'
      })
      return false;
    }
    if (that.data.memberinfo.jifen > jifen) {
      util.jhxLoadShow("加载中...");
      let nowTime = +new Date();
      if (nowTime - lastTime > 5000 || !lastTime) { //5秒内避免重复提交订单
        lastTime = nowTime;
      } else {
        return false;
      }
      var duihuanid = e.currentTarget.dataset.duihuanid;
      util.request(api.MarketingShoppingCartBuyAdd, {
          number: 1,
          price: jine,
          pk_marketing: duihuanid,
          tablename: "mt_jifenduihuan"
        }, "POST")
        .then(function (res) {
          if (res.result == 1) {
            // 如果storage中设置了shoppingcartid，则是立即购买，否则是购物车购买
            wx.setStorageSync('shoppingcartid', res.data.shoppingcartid);
            wx.navigateTo({
              url: '/pages/marketing/mtorder/mtorder'
            })
          } else {
            wx.showToast({
              image: '/static/images/icon_error.png',
              title: res.errmsg,
              mask: true
            });
          }
        });
    } else {
      wx.showToast({
        title: '暂时不够积分兑换',
        icon: 'none',
        duration: 2000
      })
    }
  },
  /**
   * 去兑换优惠券
   */
  duihuancoupon: function (e) {
    let that = this;
    var duihuanid = e.currentTarget.dataset.duihuanid;
    var jifen = e.currentTarget.dataset.jifen;
    if (that.data.memberinfo.jifen > jifen) {
      wx.showModal({
        title: '提示',
        content: '确定要兑换优惠券吗？',
        success: function (sm) {
          if (sm.confirm) {
            util.jhxLoadShow("加载中...");
            let nowTime = +new Date();
            if (nowTime - lastTime > 5000 || !lastTime) { //5秒内避免重复提交订单
              lastTime = nowTime;
            } else {
              return false;
            }
            util.request(api.Jifenduihuancoupon, {
                duihuanid: duihuanid,
                jifen: jifen
              })
              .then(function (res) {
                if (res.result == 1) {
                  wx.navigateTo({
                    url: '/pages/ucenter/jifenduihuan/jifenduihuan'
                  })
                } else {
                  wx.showToast({
                    image: '/static/images/icon_error.png',
                    title: res.errmsg,
                    mask: true
                  });
                }
              });
          } else if (sm.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      wx.showToast({
        title: '暂时不够积分兑换',
        icon: 'none',
        duration: 2000
      })
    }
  },
  /**
   * 查看兑换记录
   */
  duihuanjilu: function (e) {
    wx.navigateTo({
      url: '/pages/ucenter/jifenduihuan/jifenduihuan'
    })
  },

  /**
   * 积分
   */
  gotojifen: function (e) {
    let token = wx.getStorageSync('token');
    if (token) {
      wx.navigateTo({
        url: e.currentTarget.dataset.url
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    };
  }
})